package com.example.composecustomthemes.customtheme

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import com.example.composecustomthemes.ui.theme.lightColors
import com.example.composecustomthemes.customtheme.CustomColors as CustomColors

class CustomColors(
    primary: Color,
    text: Color,
    background: Color,
    success: Color,
    error: Color,
    isLight: Boolean
) {

    /**
     * 为什么要使用 MutableStateOf？
     *     MutableStateOf 会创建一个值存储在缓存中，防止可组合程序每次调用该方法时都要重新组合，
     *     而且只有在其值更新时才会向用户界面发射，因此我们需要使用它来存储颜色状态并保持动态。
     */
    var primary by mutableStateOf(primary)
        private set

    var text by mutableStateOf(text)
        private set

    var background by mutableStateOf(background)
        private set

    var success by mutableStateOf(success)
        private set

    var error by mutableStateOf(error)
        private set

    var isLight by mutableStateOf(isLight)
        private set


    /**
     * 由于 MutableState 的原因，我们无法创建数据类，但我们可以创建复制和 updateColorsFrom 方法
     */
    fun copy(
        primary: Color = this.primary,
        text: Color = this.text,
        background: Color = this.background,
        success: Color = this.success,
        error: Color = this.error,
        isLight: Boolean = this.isLight
    ) = CustomColors(
        primary = primary,
        text = text,
        background = background,
        success = success,
        error = error,
        isLight = isLight
    )


    fun updateColorFrom(other: CustomColors) {
        primary = other.primary
        text = other.text
        background = other.background
        success = other.success
        error = other.error
        isLight = other.isLight
    }
}

val LocalColors = staticCompositionLocalOf { lightColors() }